<?php

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// 换行符，如果是通过http访问本页面，则换行为<br/>, 如果是通过命令行执行此脚本，换行符为\n
$seperator = PHP_SAPI == "cli" ? "\n" : "<br/>";

// 最近30天的数组
for($i = time()-30*24*3600; $i <= time(); $i += 24*3600) {
    $dates[] = date('Y-m-d', $i);
}

$dau_array = $wau_array = $mau_array = array();

foreach ($dates as $date) {
    //echo $date . " > ".$seperator;

    $register_total = $redis->bitCount('register:'.$date);

    $redis->del('register:drr'); //初始化key
    // 日留存率
    $redis->bitOp('AND', 'register:drr', 'register:'.$date, 'login:'.date('Y-m-d', strtotime($date.' 23:00:00')+24*3600));
    //echo $redis->bitCount('register:drr');exit;
    $drr_array[$date] = number_format($redis->bitCount('register:drr')*100/$register_total, 2, '.', '');

    $redis->del('register:wrr'); //初始化key
    // 周留存率
    $redis->bitOp('AND', 'register:wrr', 'register:'.$date, 'login:'.date('Y-m-d', strtotime($date.' 23:00:00')+7*24*3600));
    $wrr_array[$date] = number_format($redis->bitCount('register:wrr')*100/$register_total, 2, '.', '');

    $redis->del('register:mrr'); //初始化key
    // 月留存率
    $redis->bitOp('AND', 'register:mrr', 'register:'.$date, 'login:'.date('Y-m-d', strtotime($date.' 23:00:00')+30*24*3600));
    $mrr_array[$date] = number_format($redis->bitCount('register:mrr')*100/$register_total, 2, '.', '');

    //echo $seperator;
}

$redis->close();
?>
<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>留存度统计</title>
</head>
<body>
<!-- 为ECharts准备一个具备大小（宽高）的Dom -->
<div id="drr" style="height:400px"></div>
<div id="wrr" style="height:400px"></div>
<div id="mrr" style="height:400px"></div>
<!-- ECharts单文件引入 -->
<script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script>
<script type="text/javascript">
    // 用户日活跃度
    displayCharts('drr', '#ff7f50', '用户日留存率', '日留存率DRR', ['<?=implode("','", $dates)?>'], [<?=implode(',', $drr_array)?>]);
    // 用户周活跃度
    displayCharts('wrr', '#87cefa', '用户周留存率', '周留存率WRR', ['<?=implode("','", $dates)?>'], [<?=implode(',', $wrr_array)?>]);
    // 用户月活跃度
    displayCharts('mrr', '#da70d6', '用户月留存率', '月留存率MRR', ['<?=implode("','", $dates)?>'], [<?=implode(',', $mrr_array)?>]);
    // 活跃度图
    function displayCharts(id, color, title, legend, x_data, y_data) {
        // 基于准备好的dom，初始化echarts图表
        var myChart = echarts.init(document.getElementById(id), 'macarons');

        var option = {
            color:[color],
            title : {
                text: title
            },
            tooltip : {
                trigger: 'axis'
            },
            xAxis : [
                {
                    type : 'category',
                    boundaryGap : false,
                    data : x_data
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : [
                {
                    name:legend,
                    type:'line',
                    smooth:true,
                    itemStyle: {normal: {areaStyle: {type: 'default'}}},
                    data:y_data
                }
            ]
        };
        // 为echarts对象加载数据
        myChart.setOption(option);
    }
</script>
</body>